package com.sunelec.controller;

import com.sunelec.pojo.Power;
import com.sunelec.pojo.SunElecResult;
import com.sunelec.pojo.Users;
import com.sunelec.service.EdictPowerInfoService;
import com.sunelec.service.ShowInfoService;
import com.sunelec.service.UserService;
import com.sunelec.utils.IDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @Author: 郑明利
 * @Description: Update
 * @Date: Create in 14:27 2018/05/13
 * @Modified By:
 */
@Controller
public class EdictPowerInfoController {

    @Autowired
    private EdictPowerInfoService edictPowerInfoService;

    @Autowired
    private ShowInfoService showInfoService;


    @RequestMapping("/edictPowerInformation")
    public String edictPowerInformation(String powerId, Model model, HttpServletRequest request){
        int status = 0;
        try{
            status =Integer.parseInt(request.getParameter("status"));
            if (status == 200){
                model.addAttribute("msg","修改成功！");
            }else {
                model.addAttribute("msg", "电站信息异常,请核实");
            }
        }catch (Exception e){

        }
        Power power = edictPowerInfoService.selectByPrimaryKey(powerId);
        Users users = showInfoService.getUserInfo(power.getOwn());
        model.addAttribute("powerId",powerId);
        model.addAttribute("power",power);
        model.addAttribute("users",users);
        return "edictInformation";
    }

    @RequestMapping("/edictPowerInfo")
    public String edictPowerInfo(String powerName,String powerType, String country,
                                 String city,String local,String capacity,Integer powerAvgElec,
                                 Integer powerConCost,Integer powerConSubsidy,String phone,String repayMethod,
                                 String powerSn, HttpSession session, Model model,
                                 HttpServletRequest request){
        Power power = new Power();
        power.setPowerName(powerName);
        power.setPoweType(powerType);
        power.setPowerLocal(country+city+local);
        power.setInstallCapacity(capacity);
        power.setPowerAvgElec((float)powerAvgElec);
        power.setPowerConCost((float)powerConCost);
        power.setPowerConSubsidy((float)powerConSubsidy);
        power.setRepayMethod(repayMethod);
        power.setPowerSn(powerSn);

        SunElecResult sunElecResult = edictPowerInfoService.updatePower(power);

        return "forward:edictPowerInformation.action?status="+sunElecResult.getStatus();
    }
}
